package com.carmanage.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.carmanage.domain.Page;
import com.carmanage.domain.User;
import com.carmanage.service.UserService;


@Controller
public class UserController {
    
	@Resource
	private UserService userservice;
	

    
	private List<User> users;
	public UserService getUserservice() {
		return userservice;
	}

	public void setUserservice(UserService userservice) {
		this.userservice = userservice;
	}

	
	

	@RequestMapping(value = "/addUser")
	public void addUser(String username,String password,HttpServletResponse response) throws IOException{
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		int i = this.userservice.addUser(user);
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		if(i == 1){
			out.print("用户添加成功！");
		}else
	        out.print("用户添加失败！");
	}	
	
	
	@RequestMapping(value = "/deleteUser")
	public void deleteUserByName(String username,HttpServletResponse response)throws IOException{
        System.out.println("///deleteUserByName");
        User user = new User();
        user = findUserByName(username);
		int i = this.userservice.deleteUserById(user.getUserid());
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		if(i == 1){
			out.print("用户删除成功！");
		}else
	        out.print("用户删除失败！");
		
	}
	
	@RequestMapping(value = "/findAllUser")
	public String findAllUser(HttpSession session){
		users = userservice.findAllUser();
		session.setAttribute("AllUsers",users);
		return "redirect:/pages/userManage.jsp";
	}
	
	@RequestMapping(value = "/updateUser")
	 public void updateUser(){
		
	}
	
	@RequestMapping(value = "/findUserByName")
	public User findUserByName(String username){
		User user = new User();
		user = userservice.findUserByName(username);
		return user;
	}
	
	@RequestMapping(value = "/ifUser")
	public void ifUser(String username,String password,int roleid,HttpSession session,HttpServletResponse response) throws IOException{
		int ifuserresult = userservice.findUser(username,password,roleid);
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
	    if(ifuserresult == 1){
	    	User user = findUserByName(username);
	    	session.setAttribute("CurrentUser",user);
	    	session.setAttribute("CurrentUserRoleId",roleid);
	    	out.print("登录成功!");
	    }else if(ifuserresult == 2){
            out.print("用户不属于该角色!");
	    }else
            out.print("登录信息输入错误!");
	}
	
	@RequestMapping(value = "/findUserFirstPage")
	public String findUserFirstPage(HttpServletRequest request){
		int totalRows = userservice.findAllUserRecord();
		System.out.println(totalRows);
		Page userPage = new Page(totalRows,1); 
		request.getSession().setAttribute("UserPage",userPage);
		 users = userservice.findUserByPage(userPage);
		 request.setAttribute("UsersByPage",users);
		 System.out.println(users);
		 return "forward:/pages/userManage.jsp";   //redirect的话request传值失效
	}
	
	@RequestMapping(value = "/findUserPageBy")
	public String findUserPage(int currentPage,HttpServletRequest request){
		Page userPage = (Page) request.getSession().getAttribute("UserPage");
		userPage.setCurrentPage(currentPage);
		request.getSession().setAttribute("UserPage",userPage);
		users = userservice.findUserByPage(userPage);
		request.setAttribute("UsersByPage",users);
		return "forward:/pages/userManage.jsp";
		
	}
}
